package com.simple.portal.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.simple.base.controller.AbstractBaseController;
import com.simple.portal.bo.IGroupInfoBO;
import com.simple.portal.bo.IUserInfoBO;
import com.simple.portal.vo.GroupInfoVO;
import com.simple.portal.vo.RightInfoVO;
import com.simple.portal.vo.UserInfoVO;
import com.simple.util.TbResourceLoad;
import com.simple.util.common.Constants;
import com.simple.util.common.EncryptUtil;
import com.simple.util.common.SpringUtil;
import com.simple.util.object.PageInfoBean;
import com.simple.util.object.portal.SessionUserBean;




public abstract class AbstractPortalBaseController extends AbstractBaseController{
	
    protected SessionUserBean getCurrentSessionUserBean(final HttpServletRequest request) {
        return (SessionUserBean)request.getSession().getAttribute(Constants.CURRENT_SESSION_USERBEAN);
    }
    
    protected boolean checkRights(final HttpServletRequest request,final HttpServletResponse response,final  String ... rightIds) throws IOException{
    	boolean flag = isHaveRights(request,rightIds) ;
    	if(!flag){
    		final String goUrl = "/message/alert?errorId=message.user.have.no.rights&goPage="+Constants.SYSTEM_ALERT_GOPAGE_HOME;
        	response.sendRedirect(goUrl);

    	}
    	return flag;
    }
    
    protected  boolean isHaveRights(final HttpServletRequest request,final  String ...rightIds){
    	boolean flag = false;
    	SessionUserBean sessionUserBean =getCurrentSessionUserBean(request);
    	List<RightInfoVO> rightInfoVOList=sessionUserBean.getRightInfoVOListO();
    	for(RightInfoVO rightInfoVO : rightInfoVOList){
    		
    		for(String rightId : rightIds){
        		if(rightId.equals(rightInfoVO.getRightId())){
        			flag = true;
        			break;
        		}
    			
    		}
    		
    		if(flag){
        		break;
    		}

    	}
    	
    	return flag;
    }
    
    protected boolean checkManagerUser(final String userId,final HttpServletRequest request,final HttpServletResponse response) throws IOException{
    	
	   	boolean flag =  isManageUser(userId,request) ;
	   	if(!flag){
	   		final String goUrl = "/message/alert?errorId=message.user.have.no.rights&goPage="+Constants.SYSTEM_ALERT_GOPAGE_HOME;
	       	response.sendRedirect(goUrl);
	
	   	}
	   	return flag;
    }
    
    private  boolean isManageUser(final String userId,final HttpServletRequest request ){
    	boolean flag = false;
    	SessionUserBean sessionUserBean =getCurrentSessionUserBean(request);
    	int currentGroupLevel = Integer.valueOf(sessionUserBean.getGroupInfoVO().getGroupLevel());
    	final IUserInfoBO userInfoBO = (IUserInfoBO) SpringUtil.getBean("userInfoBO");
    	UserInfoVO userInfoVO=userInfoBO.findById(userId);
    	List<GroupInfoVO> groupInfoVOList = userInfoBO.findGroupInfoVOListByLoginId(EncryptUtil.encrypt(userInfoVO.getLoginId()));
    	int groupLevel = 1;
    	if(null != groupInfoVOList && !groupInfoVOList.isEmpty()){
    		groupLevel=Integer.valueOf(groupInfoVOList.get(0).getGroupLevel());
    	}
    	if(sessionUserBean.getUserInfoVO().getUserId().equals(userInfoVO.getCreator())){
    		 
    		if(currentGroupLevel > groupLevel){
    			flag = true;
    		}
    	}
    	return flag;
    }
    
   protected boolean checkManagerUserGroup(final String groupId,final HttpServletRequest request,final HttpServletResponse response) throws IOException{
	   
	   	boolean flag =  isManageUserGroup(groupId,request) ;
	   	if(!flag){
	   		final String goUrl = "/message/alert?errorId=message.user.have.no.rights&goPage="+Constants.SYSTEM_ALERT_GOPAGE_HOME;
	       	response.sendRedirect(goUrl);
	
	   	}
	   	return flag;
    }
    
    private  boolean isManageUserGroup(final  String groupId,final HttpServletRequest request ){
    	
    	boolean flag = false;
    	SessionUserBean sessionUserBean =getCurrentSessionUserBean(request);
    	
    	int currentGroupLevel = Integer.valueOf(sessionUserBean.getGroupInfoVO().getGroupLevel());
    	
    	final IGroupInfoBO groupInfoBO = (IGroupInfoBO) SpringUtil.getBean("groupInfoBO");
    	
    	GroupInfoVO groupInfoVO  = groupInfoBO.findById(groupId);
    	
    	int groupLevel=Integer.valueOf(groupInfoVO.getGroupLevel());
    	
    	if(sessionUserBean.getUserInfoVO().getUserId().equals(groupInfoVO.getCreator())){
    		 
    		if(currentGroupLevel > groupLevel){
    			flag = true;
    		}
    	}
    	return flag;
    }
    
    
    protected String generatePageInfo(final HttpServletRequest request, final HttpServletResponse reponse, final PageInfoBean pageInfoBean, final String goPageFunctionNname) {
        String sPageInfo = "";
        final StringBuffer pageInfo = new StringBuffer(500);
        
   
        if(pageInfoBean.getTotalPageNo()>1){
            final String locale = getSiteLanguage(request);
    		pageInfo.append("<div style='float: left'>");
            pageInfo.append(TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.total"));
            pageInfo.append(pageInfoBean.getTotalRecordCount());
            pageInfo.append(TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.pagesize.int"));
            pageInfo.append(TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.current.page.no"));
            pageInfo.append(pageInfoBean.getCurPageNo());
            pageInfo.append(TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.page"));
            pageInfo.append(TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.comma"));
            pageInfo.append(TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.total"));
            pageInfo.append(pageInfoBean.getTotalPageNo());
            pageInfo.append(TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.page")+"</div>");
            pageInfo.append("<div style='margin-left:500px;'>");
            if(pageInfoBean.getCurPageNo() >1){
                pageInfo.append("<a title='First Page' onclick='javascript:" + goPageFunctionNname+"(1)'>"+ TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.first")+"</a>");
            }
            
            if(pageInfoBean.getCurPageNo() >1){
                pageInfo.append("<a title='Previous Page' onclick='javascript:" + goPageFunctionNname+"("+(pageInfoBean.getCurPageNo()-1)+")'>"+ TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.prior")+"</a>");
            }
            
            
            int beginNo =  pageInfoBean.getCurPageNo() - (int)Math.ceil((double)PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME/2);
            if(beginNo<1){
                beginNo = 1;
            }
            
            int endNo = pageInfoBean.getCurPageNo() + (PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME-(int)Math.ceil((double)PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME/2)-1);
            if(endNo>pageInfoBean.getTotalPageNo()){
                endNo = pageInfoBean.getTotalPageNo();
            }
            
            int nShowPageCount = endNo - beginNo + 1;
            if(nShowPageCount<PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME){
                beginNo = beginNo - (PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME-nShowPageCount);
                if(beginNo<1){
                    beginNo = 1;
                }
            }
            
            nShowPageCount = endNo - beginNo + 1;
            if(nShowPageCount<PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME){
                endNo = endNo + (PageInfoBean.COMMON_PAGE_COUNT_IN_ONE_TIME-nShowPageCount);
                if(endNo>pageInfoBean.getTotalPageNo()){
                    endNo = pageInfoBean.getTotalPageNo();
                }
            }
            
            for(int i=beginNo;i<=endNo;i++){
                if(i==pageInfoBean.getCurPageNo()){
                    pageInfo.append("<a title='"+i+"' class='number current'>"+i+"</a>");
                }
                else{
                    pageInfo.append("<a title='"+i+"' class='number' onclick='javascript:" + goPageFunctionNname+"("+i+")'>"+i+"</a>");
                }
            }
            
            if(pageInfoBean.getCurPageNo() < pageInfoBean.getTotalPageNo()){
                pageInfo.append("<a title='Next Page' onclick='javascript:" + goPageFunctionNname+"("+(pageInfoBean.getCurPageNo()+1)+")'>"+ TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.next")+"</a>");
            }
            
            if(pageInfoBean.getCurPageNo() < pageInfoBean.getTotalPageNo()){
                pageInfo.append("<a title='Last Page' onclick='javascript:" + goPageFunctionNname+"("+pageInfoBean.getTotalPageNo()+")'>"+ TbResourceLoad.getInstance().getString(Constants.TBRESOURCES_PACKAGE_WEB, locale, "web.system.pageinfo.last")+"</a>");
            }
            
            pageInfo.append("</div>");
            sPageInfo = "<div class='pagination'>"+pageInfo.toString()+"</div>";
        }
        return sPageInfo;
    }
    
    
    
    

	
}
